package behavioral_pattern.observer_pattern;

import java.util.EventListener;
import java.util.EventObject;

/**
 * @version: 1.0
 * @Description: 事件监听器模式
 * @author: zZmH
 * @date: 2020/12/07 18:34
 */


public class EventPattern {

}

/**
 * 页面加载事件
 */
class LoadPageEvent extends EventObject {

    private int id; //表示具体事件的类型

    public static final int PAGELOAD_SUCCESS = 1;  //页面加载成功事件
    public static final int PAGELOAD_ERROR = 2; //页面加载失败事件
    public static final int PAGELOAD_START = 3; //页面开始加载事件
    public static final int PAGELOAD_CLOSE = 4; //页面关闭事件

    //建立LoadPageEvent实例对象，要指明具体的id类型，构造函数根据id来指定具体的事件类型
    public LoadPageEvent(Object source, int id) {
        super(source);
        switch (id) {
            case PAGELOAD_CLOSE:
                this.id = PAGELOAD_CLOSE;
                break;
            case PAGELOAD_ERROR:
                this.id = PAGELOAD_ERROR;
                break;
            case PAGELOAD_START:
                this.id = PAGELOAD_START;
                break;
            case PAGELOAD_SUCCESS:
                this.id = PAGELOAD_SUCCESS;
                break;
        }
    }

    public LoadPageEvent(Object source) {
        super(source);
    }

    //获取事件的具体的事件类型
    public int getId() {
        return this.id;
    }
}

/**
 * 点击事件
 */
class ClickEvent extends EventObject {

    //具体的事件类型
    private int id;

    public final static int SINGLE_CLICK = 1; //单击事件
    public final static int DOUBLE_CLICK = 2; //双击事件


    public ClickEvent(Object source) {
        super(source);
    }

    //建立ClickEvent实例对象，要指明具体的id类型，构造函数根据id来指定具体的事件类型
    public ClickEvent(Object source, int id) {
        super(source);
        switch (id) {
            case SINGLE_CLICK:
                this.id = SINGLE_CLICK;
                break;
            case DOUBLE_CLICK:
                this.id = DOUBLE_CLICK;
                break;
        }
    }

    public int getId() {
        return this.id;
    }

}

/**
 * 鼠标移动事件
 */

class MouseMovationEvent extends EventObject {
    //具体的事件类型
    private int id;

    public static final int MOVE_LEFT = 0; //鼠标左移事件
    public static final int MOVE_RIGHT = 1; //鼠标右移事件

    //建立MouseMovationEvent实例对象，要指明具体的id类型，构造函数根据id来指定具体的事件类型
    public MouseMovationEvent(Object source, int id) {
        super(source);
        switch (id) {
            case MOVE_LEFT:
                this.id = MOVE_LEFT;
                break;
            case MOVE_RIGHT:
                this.id = MOVE_RIGHT;
                break;
        }
    }

    public MouseMovationEvent(Object source) {
        super(source);
    }

    public int getId() {
        return this.id;
    }
}

////////////////////页面加载事件监听器//////////////////
class LoadPageListener implements EventListener {

    //LoadPageEvent的成功加载事件触发时，则执行该方法
    public void onSuccess(LoadPageEvent e) {
        System.out.println("页面加载成功");
    }

    //LoadPageEvent的开始加载事件触发时，则执行该方法
    public void onStart(LoadPageEvent e) {
        System.out.println("页面开始加载");
    }

    //LoadPageEvent的关闭事件触发时，则执行该方法
    public void onClose(LoadPageEvent e) {
        System.out.println("页面关闭");
    }

    //LoadPageEvent的异常加载事件触发时，则执行该方法
    public void onError(LoadPageEvent e) {
        System.out.println("页面加载出错");
    }
}

///////////页面点击监听器///////////////////
class ClickListener implements EventListener {

    //ClickEvent的单击事件触发时，则执行该事件
    public void onSingle(ClickEvent clickEvent) {
        System.out.println("鼠标单击");
    }

    //ClickEvent的双击事件触发时，则执行该事件
    public void onDouble(ClickEvent clickEvent) {
        System.out.println("鼠标双击");
    }
}

///////////鼠标移动监听器///////////////////
class MouseMovationListener implements EventListener {

    //MouseMovationEvent的左移事件触发时，则执行该方法
    public void onLeft(MouseMovationEvent mouseMovationEvent) {
        System.out.println("鼠标左划");
    }

    //MouseMovationEvent的右移事件触发时，则执行该方法
    public void onRight(MouseMovationEvent mouseMovationEvent) {
        System.out.println("鼠标右划");
    }
}

///////////////事件源////////////
class Page {

    private LoadPageListener loadPageListener;
    private ClickListener clickListener;
    private MouseMovationListener mouseMovationListener;

    //为自身注册事件监听器
    public void addLoadPageListener(LoadPageListener loadPageListener) {
        this.loadPageListener = loadPageListener;
    }

    public void addClickListener(ClickListener clickListener) {
        this.clickListener = clickListener;
    }

    public void addMouseMovationListener(MouseMovationListener mouseMovationListener) {
        this.mouseMovationListener = mouseMovationListener;
    }

    //触发事件
    public void trigger(EventObject e) {

        //判断事件类型，确定使用的事件监听器类，并且确定调用事件监听器类的哪个方法。
        if (e instanceof LoadPageEvent) {
            LoadPageEvent le = (LoadPageEvent) e;
            switch (le.getId()) {
                case LoadPageEvent.PAGELOAD_CLOSE:
                    this.loadPageListener.onClose(le);
                    break;
                case LoadPageEvent.PAGELOAD_ERROR:
                    this.loadPageListener.onError(le);
                    break;
                case LoadPageEvent.PAGELOAD_START:
                    this.loadPageListener.onStart(le);
                    break;
                case LoadPageEvent.PAGELOAD_SUCCESS:
                    this.loadPageListener.onSuccess(le);
                    break;
                default:
                    break;
            }
        } else if (e instanceof ClickEvent) {
            ClickEvent ce = (ClickEvent) e;
            switch (ce.getId()) {
                case ClickEvent.DOUBLE_CLICK:
                    this.clickListener.onDouble(ce);
                    break;
                case ClickEvent.SINGLE_CLICK:
                    this.clickListener.onSingle(ce);
                    break;
                default:
                    break;
            }
        } else if (e instanceof MouseMovationEvent) {
            MouseMovationEvent me = (MouseMovationEvent) e;
            switch (me.getId()) {
                case MouseMovationEvent.MOVE_LEFT:
                    this.mouseMovationListener.onLeft(me);
                    break;
                case MouseMovationEvent.MOVE_RIGHT:
                    this.mouseMovationListener.onRight(me);
                    break;
                default:
                    break;
            }
        }
    }
}